package framework.ui;


import java.util.List;

import org.openems.entity.Key;

import framework.DomainService;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;

public class EmsComboBox extends Pane {
	private IComboListener listener;
	
	public EmsComboBox(Label title, ChoiceBox<String> choices, Key key, DomainService service) {
		initialize(key, service, title, choices);
	}
	
	public EmsComboBox(final Key key, DomainService service) {
		HBox hbox = new HBox();
		this.getChildren().add(hbox);
		final Label title = new Label();
		hbox.getChildren().add(title);
		final ChoiceBox<String> choice = new ChoiceBox<String>();
		hbox.getChildren().add(choice);
		
		initialize(key, service, title, choice);
	}

	private void initialize(final Key key, DomainService service,
			final Label title, final ChoiceBox<String> choice) {
		choice.getSelectionModel().selectedIndexProperty().addListener(new ChangeListener<Number>() {
			@Override
			public void changed(ObservableValue<? extends Number> arg0,
					Number arg1, Number arg2) {
				listener.changed(arg2.intValue());
			}
		});
	
		ILabel label = new ILabel() {
			@Override
			public void setText(String name) {
				title.setText(name);
			}
		};
		
		new EmsComboConnector(label, new IComboBox() {
			@Override
			public void setElements(List<String> choices) {
				choice.itemsProperty().setValue(FXCollections.observableArrayList(choices));
			}

			@Override
			public void setOnChange(IComboListener iComboListener) {
				listener = iComboListener;
			}

			@Override
			public void setSelectedIndex(Integer index) {
				choice.getSelectionModel().select(index);
			}
			
		}, service, key);
	}
}
